# -*- coding: utf-8 -*-
import scrapy
import re
from copy import deepcopy


class BmSpiderSpider(scrapy.Spider):
    name = 'bm_spider'
    allowed_domains = ['music.baidu.com']
    start_urls = ['http://music.baidu.com/songlist']

    # 获取页面的专辑列表
    def parse(self, response):
        li_list = response.xpath('//div[@class="songlist-list"]/ul/li')
        for li in li_list:
            item = {}
            item["sl_title"] = li.xpath('./p[@class="text-title"]/a/text()').extract_first()  # 专辑标题
            item["sl_listen_number"] = li.xpath('.//div[@class="num"]/span/text()').extract_first()  # 专辑收听量
            item["sl_listen_number"] = re.sub(r'\s+', '', item["sl_listen_number"])  # 匹配任意的空白符
            item["sl_create_people"] = li.xpath('./p[@class="text-user"]/a/@title').extract_first()  # 专辑编辑者
            # print(item)
            item["sl_href"] = li.xpath('./p[@class="text-title"]/a/@href').extract_first()
            if item["sl_href"] is not None:
                item["sl_href"] = "http://music.baidu.com" + item["sl_href"]  # 专辑歌单详情地址
                yield scrapy.Request(
                    item['sl_href'],  # 详情页地址
                    callback=self.parse_detail_list,  # 回调函数
                    meta={'item': deepcopy(item)}
                )

        # 下一页
        next_url = response.xpath('//a[text()="下一页>"]/@href').extract_first()
        if next_url is not None:
            next_url = "http://music.baidu.com" + re.sub(r'\s+', '', next_url)
            print(next_url)
            yield scrapy.Request(
                next_url,
                callback=self.parse
            )

    # 获取详情页的数据
    def parse_detail_list(self, response):
        item = response.meta['item']
        li_music_list = response.xpath(
            "//div[@class=\"normal-song-list song-list song-list-hook clear song-list-btnBoth  song-list-btnTop "
            "song-list-btnBottom\"]/ul/li")
        for li in li_music_list:
            item["music_name"] = li.xpath('.//span[@class="song-title "]/a/text()').extract_first()
            item["music_singer"] = li.xpath('.//span[@class="author_list"]/a/text()').extract_first()
            item["music_special"] = li.xpath('.//span[@class="album-title"]/a/text()').extract_first()
            item["music_href"] = "http://music.baidu.com" + li.xpath('.//span[@class="song-title "]/a/@href'). \
                extract_first()
            # print(item)
            yield item
